package com.saclub.app.handler;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;

import com.offroader.utils.JsonUtils;
import com.saclub.app.R;
import com.saclub.app.adapter.AttitudeListAdapter;
import com.saclub.app.adapter.NewsAdapter;
import com.saclub.app.adapter.SaclubListAdapter;
import com.saclub.app.bean.page.NewsListPage;
import com.saclub.app.bean.part.News;
import com.saclub.app.common.Constants.NewsTypeEnum;
import com.saclub.app.common.MyPageHelper;
import com.saclub.app.fragment.CommonListFragment;
import com.saclub.app.fragment.MainTabAttitudeFragment;
import com.saclub.app.fragment.MainTabSaClubFragment;
import com.saclub.app.fragment.NewsListFragment;

public class NewsResponseHandler extends MyBaseHttpResponseHandler<NewsListFragment, NewsListPage> implements OnItemClickListener {

	private BaseAdapter adapter;

	@Override
	public void deal() {

		caller.getList().addAll(page.getNewslist());

		if (caller instanceof MainTabSaClubFragment) {

			adapter = new SaclubListAdapter(caller.getList());

		} else if (caller instanceof MainTabAttitudeFragment) {

			adapter = new AttitudeListAdapter(caller.getList());

		} else if (caller instanceof CommonListFragment) {

			adapter = new NewsAdapter(caller.getList());

		}

		caller.setAdapter(adapter);
		caller.getListview().setAdapter(adapter);
		caller.getListview().setOnItemClickListener(this);

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		News news = caller.getList().get(position);

		String url = news.getNewUrl();
		String title = news.getNewTitle();

		Bundle args = new Bundle();
		args.putString("news", JsonUtils.toJson(news));
		
		if(caller instanceof CommonListFragment){
			
			CommonListFragment frag = (CommonListFragment)caller;
			args.putString("from", frag.getFrom());
			
			//设置标题
//			title =  NewsTypeEnum.getByType(frag.getTypeId()).getName();
			title =  news.getSectionName();
			
		}else if(caller instanceof MainTabSaClubFragment){
			
			//设置标题
			title = caller.getString(R.string.main_tab_saclub_title);
			args.putString("from", "saclub");
			
		}else if(caller instanceof MainTabAttitudeFragment){
			
			//设置标题
			title = caller.getString(R.string.main_tab_attitude_title);
			
		}
		

		MyPageHelper.webView.showMyWebViewPage(caller.getActivity(), url, title, args);

	}

}
